package yxy.game.pm2.active.command.flop;

import yxy.apple.logger.Logger;
import yxy.cherry.data.bean.actflopstagereward;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_翻牌;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.subordinateclass.flop.FlopActive;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.Collection;

/**
 * 翻牌 - 活跃任务 宝箱
 * @author Dell
 *
 */
@MessageType(proto = Command.ActFlopCardBoxDrawReward, classtype = ActiveMessage.ActFlopCardBoxDrawRewardRequest.class)
public class CMD_Active_翻牌_活跃任务_BoxDrawReward extends Command<ActiveMessage.ActFlopCardBoxDrawRewardRequest> {

	private static final Logger logger = Logger.create(CMD_Active_翻牌_活跃任务_BoxDrawReward.class);
	
	@Override
	public void execute(MessageSource messageSource, ActiveMessage.ActFlopCardBoxDrawRewardRequest request) throws Exception {
		String playercode = messageSource.getTag();
		int id = request.getId();
		ActiveMessage.ActFlopCardBoxDrawRewardResponse.Builder response = ActiveMessage.ActFlopCardBoxDrawRewardResponse.newBuilder();
		
		ActiveModule_翻牌 am = ActiveModule_翻牌.get(playercode,false);
		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}

		if(!am.running()) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}

		FlopActive fa = am.getFa();
		
		//活跃任务完成数宝箱
		if(fa.getFinishactivetasknumboxgetids().contains(id)) {
			send(messageSource, response, Result.ActHadGet);
			return;
		}
		
		actflopstagereward data = ActDataCenter.getData(id, actflopstagereward.class, playercode);
		if(data == null) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		if(fa.getFinishactivetasknum() < data.getCondition()) {
			send(messageSource, response, Result.ActCannotGet);
			return;
		}
		
		Currency cur = Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum());
		Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(cur);
		response.addAllCurs(items);
		
		fa.getFinishactivetasknumboxgetids().add(id);
		am.set();
		
		new CurrencyHandle().addCurrency(playercode, CCReason.活动_翻牌, cur);
		
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, ActiveMessage.ActFlopCardBoxDrawRewardResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}
